package com.token.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.token.converter.TestUserConverter;
import com.token.mapper.UsersMapper;
import com.token.model.dto.UsersDTO;
import com.token.model.po.Users;
import com.token.model.vo.UsersVO;
import com.token.service.TestUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @auther: henriy
 * @Date: 2023/11/29
 */
@Slf4j
@Service
public class TestUserServiceImpl implements TestUserService {

    @Resource
    private UsersMapper usersMapper;

    @Resource
    private TestUserConverter testUserConverter;

    @Override
    public void addUser(UsersDTO usersDTO) {

        Users users = new Users();
        BeanUtils.copyProperties(usersDTO, users);
        usersMapper.insert(users);
    }

    @Override
    public UsersVO selectUser(Long id) {
        Users users = usersMapper.selectById(id);
        System.out.println(users);
        UsersVO usersVO = testUserConverter.toVO(users);
        System.out.println(usersVO);
        return usersVO;
    }

    @Override
    public List<UsersVO> selectDecrypt(String address) {
        LambdaQueryWrapper<Users> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.apply(" AES_DECRYPT(UNHEX(address), 'shc987654321camp') like '%" + address + "%'");
        List<Users> usersList = usersMapper.selectList(queryWrapper);
        return usersList.stream()
                .map(testUserConverter::toVO)
                .collect(Collectors.toList());
    }

}
